/**
 * 自定义函数
 * */


//列表
function getIndex(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shequ/index',
        data: {},
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].areastr + '</td>';
        h += '<td>' + d[i].map + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'shequ-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id +'" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="pro_id' + d[i].id +'" value="' + d[i].pro_id + '"/>';
        h += '<input type="hidden" id="city_id' + d[i].id +'" value="' + d[i].city_id + '"/>';
        h += '<input type="hidden" id="area_id' + d[i].id +'" value="' + d[i].area_id + '"/>';
        h += '<input type="hidden" id="map' + d[i].id +'" value="' + d[i].map + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//选择省
function proList(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shequ/arealist',
        data: { pid: 0 },
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                return
            }
            var d = res.data;
            var h = '', tbody = $('#pro_id');
            for (var i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '</option>';
            }
            tbody.html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//选择市
function getCity(v){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shequ/arealist',
        data: { pid: v },
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                return
            }
            var d = res.data;
            var h = '', tbody = $('#city_id');
            for (var i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '</option>';
            }
            tbody.html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//选择区
function getArea(v){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shequ/arealist',
        data: { pid: v },
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                return
            }
            var d = res.data;
            var h = '', tbody = $('#area_id');
            for (var i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '</option>';
            }
            tbody.html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var icon = $('#icon' + id).val();
    tt = tt ? escape(tt) : '';
    icon = icon ? icon : '';
    url += '&title=' + tt;
    url += '&icon=' + escape(icon);
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#title')[0].value = title;
    $('#map')[0].value = map;
    var h = '';
    if (type == 0) {
        h = '<option value="0" selected>-选择-</option>';
    } else {
        h = '<option value="0">-选择-</option>';
    }
    if (type == 1) {
        h += '<option value="1" selected>个人码</option>';
    } else {
        h += '<option value="1">个人码</option>';
    }
    if (type == 2) {
        h += '<option value="2" selected>商业街</option>';
    } else {
        h += '<option value="2">商业街</option>';
    }
    if (type == 3) {
        h += '<option value="3" selected>跑腿</option>';
    } else {
        h += '<option value="3">跑腿</option>';
    }
    $('#type').html(h);
    if (img != '') {
        h = '<img src="/' + img + '"/>';
        h += '<input type="hidden" id="img" value="' + img + '"/>';
        $('#bigShow').html(h);
    }
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//修改
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        area_id: $('#area_id').val(),
        map: $('#map').val(),
    };
    if (formData.title == '') {
        msgPop({msg: '名称必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shequ/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'shequ/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}